package com.crazy.crazybusinessservice.message;//package com.crazy.crazytransactionservice.message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Date: 2024-05-13-14:38
 * @Author: Zheng Zeliang
 */
@Component
public class MqProducer {
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    RedisTemplate redisTemplate;
    public String sendMessage(String exchange, String routingKey, Integer transactionId){
        Map<String,Integer> message = new HashMap<>();
        // 生成消息的ID，放到redis里，用来判断消息是否被消费成功
        String messageId = UUID.randomUUID().toString();
        message.put(messageId,transactionId);
        rabbitTemplate.convertAndSend(exchange,routingKey,message);
        //redisTemplate.opsForValue().set(messageId,"pending");
        return messageId;
    }
}
